package com.exam.scjp5.objectives.section_4;

public class ExampleThreadLock
{
    public static void main(String args[])
    {
        SharedObjectJob job = new SharedObjectJob(new SharedObject());
        Thread t1 = new Thread(job, "Thread-1");
        Thread t2 = new Thread(job, "Thread-2");
        Thread t3 = new Thread(job, "Thread-3");
        
        t1.start();
        t2.start();
        t3.start();
    }
}

class SharedObject 
{
    int sharedValue = 0;
 
    
    public void increment()
    {
        synchronized(this)
        {
            sharedValue++;
            System.out.println(Thread.currentThread().getName() + " : " + this.sharedValue);
            try
            {
                Thread.sleep(1);
                //this.wait(5000);
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

class SharedObjectJob implements Runnable
{
    SharedObject sharedObject = null;
    
    public SharedObjectJob(SharedObject so)
    {
        sharedObject = so;
    }
    
    @Override
    public void run()
    {
        // TODO Auto-generated method stub
        this.sharedObject.increment();
    }
}
